package com.cws4cloud.cws.sdAdmin.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.sdAdmin.api.entity.SysWebsite;
import com.cws4cloud.cws.sdAdmin.api.service.SysWebsiteService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author zhulj
 * @date 2021-03-05 15:07:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/syswebsite" )
@Api(value = "syswebsite", tags = "站点管理")
public class SysWebsiteController {

    private final  SysWebsiteService sysWebsiteService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sysWebsite 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('syswebsite_get')" )
    public R getSysWebsitePage(Page page, SysWebsite sysWebsite) {
    	if (sysWebsite.getParentId()!=null) {
			return R.ok(sysWebsiteService.page(page, Wrappers.query(sysWebsite)));
		} else {
    		return R.ok(sysWebsiteService.page(page, Wrappers.query(sysWebsite).lambda().isNull(SysWebsite::getParentId)));
		}
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('syswebsite_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(sysWebsiteService.getById(id));
    }
	/**
	 * 返回树形站点
	 * @param parentId 父节点ID
	 * @return 树形站点
	 */
	@GetMapping(value = "/tree")
	public R getTree(boolean lazy, Integer parentId, String name) {
		return R.ok(sysWebsiteService.listWebsiteTrees(lazy, parentId, name));
	}

    /**
     * 新增
     * @param sysWebsite 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('syswebsite_add')" )
    public R save(@RequestBody SysWebsite sysWebsite) {
        return R.ok(sysWebsiteService.save(sysWebsite));
    }

    /**
     * 修改
     * @param sysWebsite 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('syswebsite_edit')" )
    public R updateById(@RequestBody SysWebsite sysWebsite) {
        return R.ok(sysWebsiteService.updateById(sysWebsite));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('syswebsite_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(sysWebsiteService.removeById(id));
    }

}
